package com.letfox.ad.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.GetObjectRequest;
import com.letfox.ad.model.OSSConstant;

public class OSSUtil {	
	
	public static OSSClient getInstance() {  
      
       return new OSSClient(OSSConstant.ENDPOINT, OSSConstant.ACCESSKEYID, OSSConstant.ACCESSKEYSECRET); 
	}

	
	/**
	 * 上传文件
	 * @throws FileNotFoundException
	 */
	public static void ossUpload(OSSClient ossClient,InputStream file,String fileName) throws FileNotFoundException{
		//长传文件
		ossClient.putObject(OSSConstant.BUCKETNAME, OSSConstant.IMAGES+fileName, file);
	}
	
	/**
	 * 获取图片URL
	 * @return 
	 * @throws IOException 
	 */
	public static String ossURL(OSSClient ossClient,String fileString) throws IOException{
		Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10); 
		//ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
		URL url = ossClient.generatePresignedUrl(OSSConstant.BUCKETNAME, OSSConstant.IMAGES+fileString,expiration);  
		return url.toString();
	}
	
	/**
	 * 文件下载到本地
	 */
	public static void ossDownload(OSSClient ossClient){		
		//本地url
		String url="C:\\Users\\Administrator\\Desktop\\图片\\111.jpg";
		// 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖，不存在则新建。
		ossClient.getObject(new GetObjectRequest(OSSConstant.BUCKETNAME, "图片/26Mon Jun 11 15:16:31 CST 2018.jpg"), new File(url));
		// 关闭Client。
		ossClient.shutdown();
	}
	
	/**
	 * 删除文件
	 * @param ossClient
	 * @param keys
	 * @return
	 */
	public static List<String> ossDelete(OSSClient ossClient,List<String> keys){
		DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(OSSConstant.BUCKETNAME).withKeys(keys));
		List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
		return deletedObjects;
	}
	
	/**
	 * 设置文件权限
	 * @param ossClient
	 * @param fileName
	 * @param cannedAccess
	 */
	public static void ossFileACL(OSSClient ossClient,String fileName,String cannedAccess){
		ossClient.setObjectAcl(OSSConstant.BUCKETNAME, fileName, getOSSACL(cannedAccess));
	}
	
	public static CannedAccessControlList getOSSACL(String cannedAccess){
		CannedAccessControlList cannedAccessControlList=null;
		if(cannedAccess==OSSConstant.DEFAULT)
			cannedAccessControlList=CannedAccessControlList.Default;
		if(cannedAccess==OSSConstant.PRIVATE)
			cannedAccessControlList=CannedAccessControlList.Private;
		if(cannedAccess==OSSConstant.PUBLIC_READ_WRITE)
			cannedAccessControlList=CannedAccessControlList.PublicReadWrite;
		if(cannedAccess==OSSConstant.PUBLIC_READ)
			cannedAccessControlList=CannedAccessControlList.PublicRead;
		return cannedAccessControlList;		
	}
}

